package org.lex.net.ftp;

import org.lex.io.filesystem.FileNameUtilities;
import org.lex.utils.text.SizeFormat;


public class FTPFile implements IFTPFile {
	public static final FTPFile FAKE_ROOT = FTPFile.newFile("/", "/", true);
	public static final FTPFile FAKE_FILE = FTPFile.newFile("", "", false);
	public static final FTPFile[] EMPTY_ARRAY = new FTPFile[0];
	// �ļ���name���ļ���parent��Ŀ¼
	String directory = "";
	String name = "";
	long size = -1L;
	long date = 0L;
	String attribute = "";
	boolean isDirectory = false;

	public static FTPFile newFile(String directory, String name, boolean dir) {
		FTPFile entry = new FTPFile();
		entry.directory = directory;
		entry.name = name;
		entry.isDirectory = dir;
		return entry;
	}

	FTPFile() {
		// package-private constructor
	}

	public FTPFile(String directory, String name, long size, long date, String attribute, boolean isDirectory) {
		this.directory = directory;
		this.name = name;
		this.size = size;
		this.date = date;
		this.attribute = attribute;
		this.isDirectory = isDirectory;
	}

	/*
	 * getters,this class has no public-setters
	 */
	public String getName() {
		return name;
	}

	public String getDirectory() {
		return directory;
	}

	public void setDirectory(String dir) {
		this.directory = dir;
	}

	public long getSize() {
		return size;
	}

	public long getDate() {
		return date;
	}

	public String getAttribute() {
		return attribute;
	}

	public boolean isDirectory() {
		return isDirectory;
	}

	public boolean isFile() {
		return !isDirectory;
	}

	// derived getters
	public String getFullPath() {
		return FileNameUtilities.getFullPath(directory, name, '/');
	}

	public String getParent() {
		return FileNameUtilities.getParentPath(this.directory, '/');
	}

	// toString()
	public String debugString() {
		StringBuilder buffer = new StringBuilder(100);
		if (true == this.isDirectory)
			buffer.append("<Dir>\t");
		else
			buffer.append("<File>\t").append(SizeFormat.autoFormat(size)).append("\t");
		buffer.append(this.directory);
		if (false == FileNameUtilities.isStandardPath(this.directory, '/'))
			buffer.append('/');
		buffer.append(name);
		return buffer.toString();
	}

	public String toString() {
		return FileNameUtilities.getFullPath(directory, name, '/');
	}
}
